package com.kaili.basic.controller.web.v1;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseMeasurementAddDto;
import com.kaili.basic.pojo.dto.BaseMeasurementQueryDto;
import com.kaili.basic.pojo.entity.BaseMeasurement;
import com.kaili.basic.pojo.entity.BaseMeasurementExport;
import com.kaili.basic.service.BaseMeasurementService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;


@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "单位管理", description = "单位管理相关API")
public class BaseMeasurementController {

    @Autowired
    private BaseMeasurementService measurementService;

    @PostMapping("/measurements")
    @ApiOperation(value = "新增", notes = "新增单位管理数据")
    public RestStatus addBaseMeasurement(@Validated @RequestBody BaseMeasurementAddDto baseMeasurementAddDto) {
        if (baseMeasurementAddDto == null) {
            return CommonStatus.BAD_REQUEST;
        }
        measurementService.addMeasurement(baseMeasurementAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/measurements/{measurementId}")
    @ApiOperation(value = "删除", notes = "删除单位管理数据")
    public RestStatus deleteBaseMeasurement(@PathVariable("measurementId") Long measurementId) {
        if (measurementId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        measurementService.deleteMeasurement(measurementId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/measurements")
    @ApiOperation(value = "更新", notes = "更新单位管理数据")
    public RestStatus updateBaseMeasurement(@Validated @RequestBody BaseMeasurementAddDto baseMeasurementAddDto) {
        if (baseMeasurementAddDto != null) {
            measurementService.updateMeasurement(baseMeasurementAddDto);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/measurements")
    @ApiOperation(value = "分页查询单位管理数据", notes = "分页查询事件")
    public Page<BaseMeasurement> queryBaseMeasurement(@ModelAttribute BaseMeasurementQueryDto baseMeasurementQueryDto) {
        return measurementService.queryMeasurement(new Page<BaseMeasurement>(baseMeasurementQueryDto.getPage(), baseMeasurementQueryDto.getPageSize()), baseMeasurementQueryDto);
    }

    @GetMapping("/measurementCodes")
    @ApiOperation(value = "编号", notes = "查询编号")
    public String getMeasurementCode(){
        String code = measurementService.getMeasurementCode();
        if (StringUtils.isEmpty(code)){
            return "00001";
        }

        String result = String.valueOf(Integer.valueOf(code)+1);
        if (result.length() == 1){
            result = "0000" + result;
        }else if (result.length() == 2){
            result = "000" + result;
        }else if (result.length() == 3){
            result = "00" + result;
        }else if (result.length() == 4){
            result = "0" + result;
        }
        return result;
    }

    @GetMapping("/measurements/export")
    @ApiOperation(value = "计量单位导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseMeasurementQueryDto queryDto) throws com.kaili.common.exception.RestException {
        List<BaseMeasurementExport> list = measurementService.queryMeasurementExport(queryDto);
        ExcelUtil.exportExcel(list,"计量单位信息","sheet1", BaseMeasurementExport.class, UUID.randomUUID().toString()+".xls",response);

    }
}
